﻿@{
    ViewBag.Title = "物料管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@model List<IOA.MES.DataAccess.VModel.VProduct>
@section Contents{
    <style>
        .selected_tree_node {
            background-color: #1E9FFF;
            color: #FFF;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-row">
            <div class="layui-col-md3" style="padding-right:15px;">
                <div class="layui-card" style="height: calc(100vh - 30px);overflow-y:auto;overflow-x:hidden;">
                    <div style="padding: 10px; box-sizing: border-box; display: flex; justify-content: space-between; ">
                        <input type="text" id="txtCategory" class="layui-input" style="flex-grow:1;margin-right:10px;">
                        <button id="btnEditCategory" class="layui-btn layui-btn-normal layui-btn-sm layuiadmin-btn-list" type="button" style="height:37px;">
                            <i class="layui-icon layui-icon-edit layuiadmin-button-btn"></i>
                        </button>
                        <button id="btnAddCategory" class="layui-btn layui-btn-normal layui-btn-sm layuiadmin-btn-list" type="button" style="height:37px;">
                            <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                        </button>
                    </div>
                    <div id="tree"></div>
                </div>
            </div>
            <div class="layui-col-md9">
                <div class="layui-card">
                    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">{{lang.Field.Keyword}}：</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="txtKey" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                </button>
                                <button id="btnAddMaterial" class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button">
                                    <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-card">
                    <div class="layui-card-body">
                        <table id="table" lay-filter="table"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    tree = layui.tree;
                var selectedCategoryId = 0;

                $(document).on("click", "#btnAddCategory, #btnEditCategory", function () {
                    const isEdit = $(this).attr("id") == "btnEditCategory";
                    layer.open({
                        type: 2,
                        shade: [0.1, '#000'],
                        area: ['550px', '350px'],
                        content: "/Material/EditMaterialCategory?parentId=" + (isEdit ? 0 : selectedCategoryId) + "&pkId=" + (isEdit ? selectedCategoryId : 0),
                        end: function () {
                            getCategoryTree();
                        }
                    });
                });
                $(document).on("keyup", "#txtCategory", function (e) {
                    if (e.keyCode == 13) {
                        getCategoryTree();
                    }
                });
                getCategoryTree();
                function getCategoryTree() {
                    layer.load();
                    $.ajax({
                        url: "/Material/QueryMaterialCategoryTree",
                        data: {
                            key: $("#txtCategory").val().trim()
                        },
                        complete: function () {
                            layer.closeAll();
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 6 });
                                return;
                            }

                            var treeData = new Array();
                            treeData.push({
                                id: 0,
                                title: "全部分类",
                                spread: true,
                                children: res.Data
                            });
                            initTree(treeData);
                        }
                    });
                }
                function initTree(treeData) {
                    tree.render({
                        elem: '#tree'
                        , data: treeData
                        , onlyIconControl: true
                        , click: function (obj) {
                            selectedCategoryId = obj.data.id;
                            $(".layui-tree-txt").removeClass("selected_tree_node");
                            $($($(obj.elem).children(".layui-tree-entry")).find(".layui-tree-txt")).addClass("selected_tree_node");
                            queryTable();
                        }
                    });
                }


                var options = {
                    elem: '#table'
                    , url: '/Material/QueryMaterials'
                    , method: "post"
                    , where: { categoryId: 0, key: "" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 80, fixed: "left" }
                        , { field: 'ProductCode', title: '料号', minWidth: 150, fixed: 'left' }
                        , { field: 'ProductName', title: '物料名称', minWidth: 200 }
                        , { field: 'CategoryCode', title: '分类编码', minWidth: 100 }
                        , { field: 'CategoryName', title: '分类名称', minWidth: 150 }
                        , { field: 'Specifications', title: '规格型号', minWidth: 200 }
                        , { field: 'MainUnitCode', title: '计量编码', minWidth: 80 }
                        , { field: 'MainUnitName', title: '计量名称', minWidth: 80 }
                        , { field: 'ProductionOrigin', title: '生产地', minWidth: 100 }
                        , { field: 'CreateTimeStr', title: '创建时间', minWidth: 160 }
                        , {
                            title: '操作', width: 100, fixed: 'right', templet: function (d) {
                                return '<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="editMaterial"><i class="layui-icon">&#xe642;</i>编辑</button>';
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.categoryId = selectedCategoryId;
                    options.where.key = $("#txtKey").val().trim();

                    if (table.index <= 0) {
                        table.render(options);
                        table.on('tool(table)', function (obj) {
                            if (obj.event == 'editMaterial') {
                                editMaterial(obj.data.PkId);
                            }
                        });
                    } else {
                        table.reload("table", options);
                    }
                }

                $("#btnAddMaterial").click(function () {
                    editMaterial(0);
                });

                function editMaterial(pkId) {
                    layer.open({
                        type: 2,
                        shade: [0.1, '#000'],
                        area: ['550px', '550px'],
                        content: "/Material/EditMaterial?pkId=" + pkId + "&categoryId=" + selectedCategoryId,
                        end: function () {
                            $("#btnSearch").click();
                        }
                    });
                }
            });
        }
    </script>
}
